package bl.client;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import po.ClientPo;
import po.Po;
import po.SystemLogPo;
import blservice.client.ClientBLService;
import vo.ClientVo;
import vo.Vo;
import dbservice.client.ClientDBService;
import dbservice.history.SystemLogDBService;

public  class ClientBL extends UnicastRemoteObject implements ClientBLService{
	private static final long serialVersionUID = 7950057412909644110L;
	private ClientDBService db;
	private SystemLogDBService log;
	
	public ClientBL(ClientDBService db,SystemLogDBService log) throws RemoteException{
		this.db = db;
		this.log=log;
	}

	public Vo addClient(ClientVo client) throws RemoteException{
		ClientPo temp=V2P(client);
		temp.set编号(generateId());
		Po result=db.add(temp);
		if(result.isSuccessed()){
			log.add(new SystemLogPo(client.getOperator(),"添加客户", client.get编号()+client.get姓名()));
		}
		return P2V(result);
	}
	
	public Vo deleteClient(ClientVo client){
		Po result=db.delete(V2P(client));
		if(result.isSuccessed()){
			log.add(new SystemLogPo(client.getOperator(),"删除客户", client.get编号()+client.get姓名()));
		}
		return P2V(result);
	}
	
	public Vo updateClient(ClientVo client){
		Po result=db.update(V2P(client));
		if(result.isSuccessed()){
			log.add(new SystemLogPo(client.getOperator(),"修改客户", client.get编号()+client.get姓名()));
		}
		return P2V(result);
	}
	
	@Override
	public List<ClientVo> getAll() throws RemoteException {
		List<ClientPo> list=db.getAll();
		List<ClientVo> result=new ArrayList<ClientVo>();
		for(ClientPo po:list){
			result.add(P2V(po));
		}
		return result;
	}
	
	private ClientPo V2P(ClientVo vo){
		ClientPo po=new ClientPo();
		po.set地址(vo.get地址());
		po.set姓名(vo.get姓名());
		po.set应付(vo.get应付());
		po.set应收(vo.get应收());
		po.set应收额度(vo.get应收额度());
		po.set电子邮箱(vo.get电子邮箱());
		po.set电话(vo.get电话());
		po.set类型(vo.get类型());
		po.set级别(vo.get级别());
		po.set编号(vo.get编号());
		po.set邮编(vo.get邮编());
		po.set默认业务员(vo.get默认业务员());
		return po;
	}
	
	private Vo P2V(Po po){
		Vo vo=new Vo();
		if(po.isSuccessed()){
			vo.successed();
		}else{
			vo.failed(po.getFailMessage());
		}
		return vo;
	}
	
	private ClientVo P2V(ClientPo po){
		ClientVo vo=new ClientVo();
		vo.set地址(po.get地址());
		vo.set姓名(po.get姓名());
		vo.set应付(po.get应付());
		vo.set应收(po.get应收());
		vo.set应收额度(po.get应收额度());
		vo.set电子邮箱(po.get电子邮箱());
		vo.set电话(po.get电话());
		vo.set类型(po.get类型());
		vo.set级别(po.get级别());
		vo.set编号(po.get编号());
		vo.set邮编(po.get邮编());
		vo.set默认业务员(po.get默认业务员());
		return vo;
	}

	@Override
	public String generateId() throws RemoteException {
		String biggest=db.getBiggestId();
		Integer intId;
		if(biggest==null){
			intId=1;
		}else{
			intId=Integer.parseInt(biggest.substring(3));
			intId++;
		}
		NumberFormat nf = new DecimalFormat("00000");
	    String _num = nf.format(intId);
        return "KH"+"-"+_num;
	}

	@Override
	public List<ClientVo> findClient(ClientVo clientVo) throws RemoteException {
		List<ClientPo> list=db.find(V2P(clientVo));
		List<ClientVo> result=new ArrayList<ClientVo>();
		for(ClientPo po:list){
			result.add(P2V(po));
		}
		return result;
	}
	
}
